package com.sanley.coronavirus.common.utils;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.jsoup.Jsoup;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: ming
 * @create: 2022-11-15 18:59
 * @program: coronavirus
 */
public class CrawlerUtils {
    public static void main(String[] args) throws IOException {
        getAllData();
    }
    public static void get() throws IOException {
        //腾讯疫情数据   https://api.inews.qq.com/newsqa/v1/query/inner/publish/modules/list?modules=localCityNCOVDataList,diseaseh5Shelf
        //使用jsoup提供的connect方法连接接口获取一个String字符串
        String resultBody = Jsoup.connect("https://api.inews.qq.com/newsqa/v1/query/inner/publish/modules/list?modules=localCityNCOVDataList,diseaseh5Shelf")
                .ignoreContentType(true)
                .execute().body();
        //将获取到的字符串转换为JSONObject格式便于解析
        JSONObject jsonObject = JSON.parseObject(resultBody);
        JSONObject resultJson = new JSONObject();
    }

    public static Map<String,Object> getAllData() throws IOException {
        //腾讯疫情数据   https://api.inews.qq.com/newsqa/v1/query/inner/publish/modules/list?modules=localCityNCOVDataList,diseaseh5Shelf
        String resultBody = Jsoup.connect("https://api.inews.qq.com/newsqa/v1/query/inner/publish/modules/list?modules=localCityNCOVDataList,diseaseh5Shelf")
                .ignoreContentType(true)
                .execute().body();
        JSONObject jsonObject = JSON.parseObject(resultBody);
        //获取data部分
        JSONObject data = jsonObject.getJSONObject("data");

        //获取高风险地区数据
        JSONArray localCityNCOVDataList = data.getJSONArray("localCityNCOVDataList");
        Map<String,Object> highCitysMap = new HashMap<>();
        System.out.println("高风险疫情地区数据");
        for(int i = 0;i < localCityNCOVDataList.size();i++){
            JSONObject highCity = localCityNCOVDataList.getJSONObject(i);
            //高风险地区疫情数据
            System.out.println(highCity);
            // 数据部分
            String city = highCity.getString("city");
            String province = highCity.getString("province");
            //本土确诊
            int local_confirm_add = highCity.getIntValue("local_confirm_add");
            //新增无症状
            int local_wzz_add = highCity.getIntValue("local_wzz_add");
            //高风险地区
            int highRiskAreaNum = highCity.getIntValue("highRiskAreaNum");
            //中风险地区
            int mediumRiskAreaNum = highCity.getIntValue("mediumRiskAreaNum");
            Map<String,Object> highCityMap = new HashMap<>();
            highCityMap.put("city",city);
            highCityMap.put("province",province);
            highCityMap.put("local_confirm_add",local_confirm_add);
            highCityMap.put("local_wzz_add",local_wzz_add);
            highCityMap.put("highRiskAreaNum",highRiskAreaNum);
            highCityMap.put("mediumRiskAreaNum",mediumRiskAreaNum);
            highCitysMap.put("name",highCityMap);
        }

        JSONObject diseaseh5Shelf = data.getJSONObject("diseaseh5Shelf");
        //获取国内34个省市的疫情数据
        JSONArray areaTree = diseaseh5Shelf.getJSONArray("areaTree");
        JSONObject allProvinces = areaTree.getJSONObject(0);
        JSONArray provinces = allProvinces.getJSONArray("children");

        Map<String,Object> provincesMap = new HashMap<>();
        System.out.println("各省份疫情数据");
        for (int i = 0;i < provinces.size();i++){
            JSONObject province = provinces.getJSONObject(i);
            //获取省份数据
            Map<String, Object> provinceMap = getCityValues(province);
            //打印省份数据
            System.out.println((String)provinceMap.get("name")+province+"\n\t城市数据：");
            provincesMap.put((String)provinceMap.get("name"),provinceMap);

            JSONArray citys = province.getJSONArray("children");
            Map<String,Object> citysMap = new HashMap<>();
            for(int j = 0;j < citys.size();j++){
                JSONObject city = citys.getJSONObject(j);
                //获取城市数据
                Map<String, Object> cityMap = getCityValues(city);
                //打印城市数据
                System.out.println("\t"+(String)cityMap.get("name")+city);
                citysMap.put((String)cityMap.get("name"),cityMap);
            }
        }

        //获取国内全国疫情数据
        Map<String,Object> chinaMap = new HashMap<>();
        JSONObject chinaTotal = diseaseh5Shelf.getJSONObject("chinaTotal");
        //已治愈人数
        int heal = chinaTotal.getIntValue("heal");
        //
        int dead = chinaTotal.getIntValue("dead");
        //新增无症状
        int localWzzAdd = chinaTotal.getIntValue("localWzzAdd");
        //当前无症状
        int nowLocalWzz = chinaTotal.getIntValue("nowLocalWzz");
        //所有病例（包括已治愈和死亡的）
        int confirm = chinaTotal.getIntValue("confirm");
        //新增病例
        int confirmAdd = chinaTotal.getIntValue("confirmAdd");
        //当前病例
        int nowConfirm = chinaTotal.getIntValue("nowConfirm");
        //本土病例
        int localConfirm = chinaTotal.getIntValue("localConfirm");
        //新增死亡
        int deadAdd = chinaTotal.getIntValue("deadAdd");
        //本土新增病例
        int localConfirmAdd = chinaTotal.getIntValue("localConfirmAdd");
        //中风险地区
        int mediumRiskAreaNum = chinaTotal.getIntValue("mediumRiskAreaNum");
        //高风险地区
        int highRiskAreaNum = chinaTotal.getIntValue("highRiskAreaNum");
        //全国新增数据
        //JSONObject chinaAdd = diseaseh5Shelf.getJSONObject("chinaAdd");
        chinaMap.put("heal",heal);
        chinaMap.put("dead",dead);
        chinaMap.put("localWzzAdd",localWzzAdd);
        chinaMap.put("nowLocalWzz",nowLocalWzz);
        chinaMap.put("confirm",confirm);
        chinaMap.put("confirmAdd",confirmAdd);
        chinaMap.put("nowConfirm",nowConfirm);
        chinaMap.put("localConfirm",localConfirm);
        chinaMap.put("deadAdd",deadAdd);
        chinaMap.put("localConfirmAdd",localConfirmAdd);
        chinaMap.put("mediumRiskAreaNum",mediumRiskAreaNum);
        chinaMap.put("highRiskAreaNum",highRiskAreaNum);

        //数据截止时间
        Date lastUpdateTime1 = diseaseh5Shelf.getDate("lastUpdateTime");

        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("provincesMap",provincesMap);
        resultMap.put("chinaMap",chinaMap);
        resultMap.put("highCitysMap",highCitysMap);
        resultMap.put("lastUpdateTime1",lastUpdateTime1);

        return resultMap;
    }

    public static Map<String,Object> getCityValues(JSONObject province){
        //各个省份的数据
        String name = province.getString("name");
        JSONObject today = province.getJSONObject("today");
        int todayConfirm = today.getIntValue("confirm");
        int wzz_add = today.getIntValue("wzz_add");
        int local_confirm_add = today.getIntValue("local_confirm_add");
        JSONObject total = province.getJSONObject("total");
        int confirm = total.getIntValue("confirm");
        int nowConfirm = total.getIntValue("nowConfirm");
        int wzz = total.getIntValue("wzz");
        //中风险地区数量
        int mediumRiskAreaNum = total.getIntValue("mediumRiskAreaNum");
        //高风险地区数量
        int highRiskAreaNum = total.getIntValue("highRiskAreaNum");
        int heal = total.getIntValue("heal");
        int dead = total.getIntValue("confirm");
        Map<String, Object> provinceMap = new HashMap<>();
        provinceMap.put("name",name);
        provinceMap.put("todayConfirm",todayConfirm);
        provinceMap.put("wzz_add",wzz_add);
        provinceMap.put("local_confirm_add",local_confirm_add);
        provinceMap.put("confirm",confirm);
        provinceMap.put("nowConfirm",nowConfirm);
        provinceMap.put("wzz",wzz);
        provinceMap.put("mediumRiskAreaNum",mediumRiskAreaNum);
        provinceMap.put("highRiskAreaNum",highRiskAreaNum);
        provinceMap.put("heal",heal);
        provinceMap.put("dead",dead);
        return provinceMap;
    }
}
